#ifndef AXBOPENGLWIDGET_H
#define AXBOPENGLWIDGET_H

#include "collisionstrategy.h"
#include "octree.h"
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QTimer>
#include <QTime>
#include <vector>
#include "threadpool.h"
#include "BezierCurve3.h"


class AXBOpenGLWidget : public QOpenGLWidget,QOpenGLFunctions_3_3_Core
{
    Q_OBJECT
public:
    enum Shape{None,ALL,COLLISION};
    enum viewmodel{ONE,THREE};
    explicit AXBOpenGLWidget(QWidget *parent = nullptr);
    ~AXBOpenGLWidget();

    void drawShape(Shape shape);
    void setWirefame(bool wireframe);
    void drawAxis();                       //绘制坐标轴
    void drawRobot();                      //绘制机器人
    void drawCollisionNode();              //绘制发生碰撞的点
    void drawAllNode();                    //绘制所有点
    void drawRay();                        //绘制激光射线
    void drawRayPoint();                   //绘制光线相交点
    void getOctreeNode(OctreeNode* octree);//获得八叉树中的所有点
    void clearOctreeNode();                //清空八叉树中的所有点
    void initViewThree();                  //初始化第三视角模式
    QMatrix4x4 setCamerView(viewmodel vm); //设置视角模式
    void scan();//激光器触发一次扫描
    void collider(); //碰撞体触发一次碰撞并返回最近点
    void getRayIntersectPoints();
    void buildScene();//场景构建
    void buildObject(const Point& center, const float& halflength,const float& halfwidth,const float& halfheight,const float& accuracy);
    void buildObstacle(const Point& center,const float& accuracy);
    void getPath(const int& accuracy);
    void setRobotPos(const Point& newcenter,const float& newrotation);
    bool noCollisionPath();
protected:
    virtual void initializeGL();
    virtual void resizeGL(int w, int h);
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void wheelEvent(QWheelEvent *event);
    virtual void paintGL();
    //void keyPressEvent(QKeyEvent *event);
signals:

public slots:
    void on_timeout();  
private:
    Shape m_shape;
    viewmodel m_viewmodel;
    QOpenGLShaderProgram shaderProgram;
    QOpenGLShaderProgram shaderProgramRobot;
    QOpenGLTexture * textureWall;
    QOpenGLTexture * textureSmile;
    QOpenGLTexture * textureSmall;
    std::vector<Point*> m_scenepoints;     //场景点的集合
    std::vector<Point*> m_obstaclepoints;  //障碍物点的集合
    std::vector<Point*> m_noattpoints;      //无属性点的集合
    std::vector<Point*> openglnode;        //发生碰撞的点的集合
    std::vector<Cuboid> robotcenter;   //机器人部件
    Cuboid m_collider;                   //实际参与碰撞检测的碰撞体
    std::vector<Cuboid> lasers;        //激光器
    BezierCurve3 beziercurve;                 //贝塞尔曲线
    std::vector<Point*> m_positionpath;               //贝塞尔曲线上的点，基于此绘制曲线
    std::vector<float> m_rotationpath;        //贝塞尔曲线上点的角度，不是斜率
    size_t m_pos;                             //记录小车运行到贝塞尔曲线上的哪个点
    size_t m_bzaccuracy;                      //将贝塞尔曲线分为多少段
    double m_distance;                        //最近邻和机器人的距离
    double  totalpath;                        //在一条贝塞尔曲线上的总路程

    float m_atan;
    float m_len;
    Octree* m_octree;               //八叉树
    CollisionStrategy* m_CollisionStrategy; //采用的碰撞检测策略

    //相机设置
    QVector3D cameraPos;
    QVector3D cameraTarget;
    QVector3D cameraDirection;
    QVector3D up;
    QVector3D cameraRight;
    QVector3D camerForword;
    QVector3D cameraUp;
    QVector3D cameraFront;

    float m_rotation;  //旋转角度，角度制
    QTimer timer;
    QTime m_time;
    QPoint lastPos;
};



#endif // AXBOPENGLWIDGET_H
